/**
 * OSAmI: Open Source Ambient Intelligence
 * Servlets running on the gateway, for displaying data from the sensor nodes.
 * Copyright (C) Jonathan Ferreira 2010 (internship in LCIS-INPG, for a demo in the FR part of the OSAMI project)
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
 */
package org.osami.gateway.display.internal;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Unbind;
import org.osami.gateway.sensor.SenseData;
import org.osami.gateway.storage.ISenseDataStorage;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;

@Component
@Instantiate
public class DataSensorServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private ISenseDataStorage storage;

    @Bind
    public void bindHttpService(HttpService httpService) throws ServletException, NamespaceException {
        httpService.registerServlet(ServletPaths.DATAS, this, null, null);
    }

    @Unbind
    public void unbindHttpService(HttpService httpService) {
        httpService.unregister(ServletPaths.DATAS);
    }

    @Bind
    public void bindSenseDataStorage(ISenseDataStorage storage) {
        this.storage = storage;
    }

    synchronized public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sensorID = request.getRequestURI();
        String[] tab = sensorID.split("/data/");
        sensorID = tab[1];

        Map<Long, Double> dataStorage = new TreeMap<Long, Double>();

        synchronized (storage) {
            for (SenseData sen : storage) {
                if (sen.getSenseId().equals(sensorID)) {
                    dataStorage.put(sen.getTimeStamp(), sen.getValue());
                }
            }
        }

        String dataChart = "[";

        for (Map.Entry<Long, Double> entry : dataStorage.entrySet()) {
            dataChart += "[" + entry.getKey() + "," + entry.getValue() + "],";
        }

        dataChart = dataChart.substring(0, dataChart.length() - 1);
        dataChart += "]";

        response.getWriter().write(dataChart);
    }
}
